﻿namespace WeixinPay.Models
{
    using Kevin.Comm.Strings;
    using System;
    using System.IO;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Xml;
    using WeixinPay.Business;

    public class ReceivedData
    {
        private readonly string appKey;

        public ReceivedData()
        {
            this.appKey = Config.AppKey;
        }

        public ReceivedData(HttpRequestBase request, int type)
        {
            this.appKey = Config.AppKey;
            if (request.RequestType == "POST")
            {
                string xml = new StreamReader(request.InputStream).ReadToEnd();

                //using (XmlReader reader = XmlReader.Create(request.InputStream))
                //{
                //    while (reader.Read())
                //    {
                //        if (!reader.IsEmptyElement)
                //        {
                //            if (reader.Name == "out_trade_no")
                //            {
                //                this.OrderNumber = reader["out_trade_no"];
                //            }
                //            else if (reader.Name == "total_fee")
                //            {
                //                this.Total_fee = reader["total_fee"];
                //            }
                //            else if (reader.Name == "transaction_id")
                //            {
                //                this.Transaction_id = reader["transaction_id"];
                //            }
                //            else if (reader.Name == "openid")
                //            {
                //                this.OpenId = reader["openid"];
                //            }
                //        }
                //    } 
                    
                //}

                this.RawXml = xml;
                XmlDocument document = new XmlDocument();
                document.LoadXml(xml);
                XmlElement doc = document.DocumentElement;
                if (doc != null)
                {
                    this.OpenId = doc.SelectSingleNode("openid").InnerText;
                    this.AppId = doc.SelectSingleNode("appid").InnerText;
                    this.IsSubscribe = doc.SelectSingleNode("is_subscribe").InnerText;
                    this.Result_code = doc.SelectSingleNode("result_code").InnerText;
                    this.NonceStr = doc.SelectSingleNode("nonce_str").InnerText;
                    this.AppSignature = doc.SelectSingleNode("sign").InnerText;
                    this.OrderNumber = doc.SelectSingleNode("out_trade_no").InnerText;
                    this.Total_fee = doc.SelectSingleNode("total_fee").InnerText;
                    this.Transaction_id = doc.SelectSingleNode("transaction_id").InnerText;
                }
                if (type == 0)
                {
                    this.CheckIsValid();
                }
            }
        }

        public void CheckIsValid()
        {
            string password = string.Format("appid={0}&appkey={1}&issubscribe={2}&noncestr={3}&openid={4}&productid={5}&timestamp={6}", new object[] { this.AppId, this.appKey, this.IsSubscribe, this.NonceStr, this.OpenId, this.ProductId, this.TimeStamp });
            string str2 = StringFunction.NoneEncrypt(password, 0);
            this.ReTempString = password;
            this.ReTempSign = str2;
            if (str2.ToUpper() == this.AppSignature.ToUpper())
            {
                this.IsValid = true;
            }
            this.IsValid = false;
        }

        public string AppId { get; set; }

        public string AppSignature { get; set; }

        public string IsSubscribe { get; set; }

        public bool IsValid { get; set; }

        public string NonceStr { get; set; }

        public string OpenId { get; set; }

        public string ProductId { get; set; }

        public string RawXml { get; set; }

        public string ReTempSign { get; set; }

        public string ReTempString { get; set; }

        public string SignMethod { get; set; }

        public string TimeStamp { get; set; }

        public string OrderNumber { get; set; }

        public string Total_fee { get; set; }

        public string Transaction_id { get; set; }

        public string Result_code { get; set; }
        
    }
}

